limsTest.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-test-edit-list-grid',
            url : limsTest.baseUrl + '/queries/raw',
            service : 'limsTestServiceImpl',
            dbTable : 'T_LIMS_TEST',
            page : true,
            columns : limsTest.getEditListColumns(),
            deleteFormatter: function (row) {
                return row.testCode || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'method-list-grid', 'analyte-list-grid', 'material-list-grid', 'equipment-list-grid'])
                }
            },
            onRowActive : function(index, row) {
                Gikam.getComp('method-list-grid').refresh({
                    url: limsTest.methodUrl + '/queries/raw',
                    requestData: {
                        testId_EQ: row.id
                    }
                });
                Gikam.getComp('test-supplement-list-grid').refresh({
                    url: limsTest.testSupplementUrl + '/queries/raw',
                    requestData: {
                        testId_EQ: row.id
                    }
                });
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'GIKAM.BUTTON.INSERT',
                        fields : limsTest.getBaseInfoFormFields(),
                        url : limsTest.baseUrl,
                        titleWidth : 100,
                    }).done(function(id) {
                        Gikam.getComp('lims-test-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('lims-test-edit-list-grid').deleteRows(limsTest.baseUrl);
                }
            }, {
                type: 'button',
                text: 'MODULE.COREDATA.TEST.BUTTON.ADD_PERSON',
                icon: 'copy',
                onClick: function () {
                    _this.batchMod('person');
                }
            }, {
                type: 'button',
                text: 'MODULE.COREDATA.TEST.BUTTON.ADD_TESTER',
                icon: 'copy',
                onClick: function () {
                    _this.batchMod('tester');
                }
            } ]
        }
    },

    batchMod: function (type) {
        var grid = Gikam.getComp('lims-test-edit-list-grid');
        var selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        var userIds = ''
        if (selections.length === 1) {
            if(type === 'person'){
                userIds = selections[0].personId
            } else {
                userIds = selections[0].testerId
            }
        }
        Gikam.create('modal', {
            title: Gikam.choose.getConfig('user').title,
            url: IFM_CONTEXT + Gikam.choose.getConfig("mult-user").url + Gikam.param({
                userIds: userIds,
                status_EQ: 'activated',
            }),
            onAfterClose: function (rows) {
                if (Gikam.isEmpty(rows)) {
                    return;
                }
                // 拼接人员ID和人员name
                var ids = rows.map(function (item) {
                    return item.id
                }).join(",");
                var names = rows.map(function (item) {
                    return item.userName
                }).join(",");

                var chooseData = selections.map(function (item) {
                    if(type === 'person'){
                        return {
                            id : item.id,
                            personId: ids,
                            personName: names
                        }
                    } else {
                        return {
                            id : item.id,
                            testerId: ids,
                            testerName: names
                        }
                    }
                })
                var idList = selections.map(function (item) {
                    return item.id
                });
                Gikam.put(limsTest.baseUrl + '/instant', Gikam.getJsonWrapper({}, [null, chooseData])).done(function (data) {
                    grid.refreshRowById(idList);
                });

            }
        });
    },

    getRightTab: function() {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'T_LIMS_TEST_METHOD.SERVICE_NAME',
                items: [_this.getTestMethodGridParam()]
            }, {
                title: 'T_LIMS_TEST_SUPPLEMENT.SERVICE_NAME',
                items: [_this.getTestSupplementGrid()]
            }]
        }
    },

    getTestSupplementGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'test-supplement-list-grid',
            service: 'limsTestSupplementServiceImpl',
            columns: limsTest.getTestSupplementColumns(),
            deleteFormatter: function (row) {
                return row.information
            },
            order: {'orderNo': 'asc'},
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    var activedRow = Gikam.getComp('lims-test-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                        return false;
                    }
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsTest.getTestSupplementFormFields(activedRow.id),
                        url: limsTest.testSupplementUrl,
                        param: {
                            testId: activedRow.id,
                        },
                        disableNotEmptyValidate: true
                    }).done(function (id) {
                        Gikam.getComp('test-supplement-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('test-supplement-list-grid').deleteRows(limsTest.testSupplementUrl);
                }
            }]
        }
    },

    getTestMethodGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'method-list-grid',
            height: '300',
            service: 'limsTestMethodServiceImpl',
            columns: limsTest.getTestMethodColumns(),
            deleteFormatter: function (row) {
                return row.ext$.methodcode || row.id;
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    Gikam.getComp('method-list-grid').activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'analyte-list-grid', 'material-list-grid', 'equipment-list-grid'])
                }
            },
            onRowActive: function (index, row) {
                Gikam.getComp('analyte-list-grid').refresh({
                    url: limsTest.methodAnalytesUrl + '/queries/raw',
                    requestData: {
                        testMethodId_EQ: row.id
                    }
                });
                Gikam.getComp('material-list-grid').refresh({
                    url: limsTest.methodMaterialsUrl + '/queries/raw',
                    requestData: {
                        testMethodId_EQ: row.id
                    }
                });
                Gikam.getComp('equipment-list-grid').refresh({
                    url: limsTest.methodEquipmentsUrl + '/queries/raw',
                    requestData: {
                        testMethodId_EQ: row.id
                    }
                });
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add-row',
                onClick: function () {
                    var activedRow = Gikam.getComp('lims-test-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                        return false;
                    }
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig("method").title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("method").url + Gikam.param({
                            target_filter: encodeURI(JSON.stringify({
                                type: 'unassigned ',
                                targetTable: 'T_LIMS_TEST_METHOD',
                                targetMatchColumn: 'METHODID',
                                filter: [{
                                    targetFilterColumn: 'TESTID',
                                    targetFilterValue: activedRow.id
                                }]
                            }))
                        }),
                        width: 600,
                        height: 600,
                        onAfterClose: function (rows) {
                            if (Gikam.isEmpty(rows)) {
                                return;
                            }
                            // 若原先无默认SOP，则从新添加的SOP中选择第一个做为默认
                            var hasFlag = false;
                            var oldData = Gikam.getComp('method-list-grid').getData();
                            if (Gikam.isEmpty(oldData)) {
                                hasFlag = false;
                            } else {
                                Gikam.each(oldData, function (index, item) {
                                    if (item.defaultMethod === '1') {
                                        hasFlag = true;
                                    }
                                });
                            }
                            var chooseData = rows.map(function (item) {
                                var defaultMethod = '0';
                                if (!hasFlag) {
                                    hasFlag = true;
                                    defaultMethod = "1";
                                }
                                return {
                                    'testId': activedRow.id,
                                    'methodId': item.id,
                                    'defaultMethod': defaultMethod,
                                }
                            });

                            Gikam.getComp('method-list-grid').insert(limsTest.methodUrl, chooseData).done(function () {
                                Gikam.getComp('method-list-grid').refresh();
                            });

                        }
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('method-list-grid').deleteRows(limsTest.methodUrl);
                }
            }]
        }
    },

    getAnalyteGridParam: function () {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'T_LIMS_TEST_METHOD_ANALYTE.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: 'analyte-list-grid',
                    service: 'limsTestMethodAnalyteServiceImpl',
                    deleteFormatter: function (row) {
                        return row.analyte || row.id;
                    },
                    order: {'sorter': 'asc'},
                    columns: limsTest.getAnalisisItemColumns(),
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add-row',
                        onClick: function () {
                            var activedRow = Gikam.getComp('method-list-grid').getActivedRow();
                            if (Gikam.isEmpty(activedRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                                return false;
                            }
                            Gikam.preInsert({
                                modalTitle: 'GIKAM.BUTTON.INSERT',
                                fields: limsTest.getAnalyteFormFields(),
                                titleWidth: 90,
                                url: limsTest.methodAnalytesUrl,
                                param: {
                                    testMethodId: activedRow.id
                                }
                            }).done(function (id) {
                                Gikam.getComp('analyte-list-grid').refresh();
                            });
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove-row',
                        onClick: function () {
                            Gikam.getComp('analyte-list-grid').deleteRows(limsTest.methodAnalytesUrl);
                        }
                    }],
                }]
            }, {
                title: 'T_LIMS_TEST_METHOD_MATERIAL.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: 'material-list-grid',
                    service: 'limsTestMethodMaterialServiceImpl',
                    deleteFormatter: function (row) {
                        return row.matCode;
                    },
                    columns: limsTest.getMaterialItemColumns(),
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add-row',
                        onClick: function () {
                            var activedRow = Gikam.getComp('method-list-grid').getActivedRow();
                            if (Gikam.isEmpty(activedRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                                return false;
                            }
                            Gikam.create('modal', {
                                title: 'T_LIMS_MATERIAL.TIP.CHOOSE_PAGE',
                                url: IFM_CONTEXT + '/secure/basemodule/rm/materials/page/choose-list' + Gikam.param({
                                    status_EQ: '1',
                                }),
                                width: '80%',
                                height: '80%',
                                onAfterClose: function (rows) {
                                    if (!Gikam.isEmpty(rows)) {
                                        var chooseData = rows.map(function (item) {
                                            return {
                                                testMethodId: activedRow.id,
                                                matCode: item.matCode,
                                                matName: item.matName,
                                            }
                                        });
                                        Gikam.getComp('material-list-grid').insert(limsTest.methodMaterialsUrl, chooseData)
                                    }
                                }
                            })

                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove-row',
                        onClick: function () {
                            Gikam.getComp('material-list-grid').deleteRows(limsTest.methodMaterialsUrl);
                        }
                    }],
                }]
            },{
                title: 'T_LIMS_TEST_METHOD_EQUIPMENT.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: 'equipment-list-grid',
                    page: true,
                    service: 'limsTestMethodEquipmentServiceImpl',
                    deleteFormatter: function (row) {
                        return row.equipCode || row.id;
                    },
                    columns: limsTest.getEquipmentItemColumns(),
                    toolbar: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.INSERT',
                        icon: 'add-row',
                        onClick: function () {
                            var activedRow = Gikam.getComp('method-list-grid').getActivedRow();
                            if (Gikam.isEmpty(activedRow)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                                return false;
                            }
                            Gikam.create('modal', {
                                title: 'T_LIMS_EQUIPMENT.TIP.CHOOSE_PAGE',
                                url: IFM_CONTEXT + '/secure/basemodule/rm/equipments/page/choose-list' + Gikam.param({
                                    equipState_EQ: 'normal',
                                }),
                                width: '80%',
                                height: '80%',
                                onAfterClose: function (rows) {
                                    if (!Gikam.isEmpty(rows)) {
                                        var chooseData = rows.map(function (item) {
                                            return {
                                                testMethodId: activedRow.id,
                                                equipmentId: item.id,
                                                equipCode: item.equipCode,
                                                equipName: item.equipName,
                                            }
                                        });
                                        Gikam.getComp('equipment-list-grid').insert(limsTest.methodEquipmentsUrl, chooseData)
                                    }
                                }
                            })
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.DELETE',
                        icon: 'remove-row',
                        onClick: function () {
                            Gikam.getComp('equipment-list-grid').deleteRows(limsTest.methodEquipmentsUrl);
                        }
                    }],
                }]
            }]
        }
    },

    popFormula: function (index) {
        var analyteGrid = Gikam.getComp('analyte-list-grid');
        var analyte = analyteGrid.getData()[index];
        Gikam.create('modal', {
            width: '70%',
            height: '80%',
            title: 'MODULE.COREDATA.TEST.TAB.MAINTENANCE_FORMULA',
            url: limsTest.baseUrl + '/page/analyte-formula/detail' + Gikam.param({
                testId: analyte.testMethodId,
                id: analyte.id
            }),
            onAfterClose: function (data) {
                analyteGrid.refreshRowByIndex(index);
            }
        })
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            renderTo: workspace.window.$dom,
            west: {
                width: '40%',
                title: 'T_LIMS_TEST.SERVICE_NAME',
                items: [this.getGridParam()]
            },
            center: {
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        // title: 'T_LIMS_TEST_METHOD.SERVICE_NAME',
                        items: [_this.getRightTab()]
                    },
                    center: {
                        items: [_this.getAnalyteGridParam()]
                    }
                }]
            }
        });
    },

    init : function() {
        this.create();
    }
};
